package com.veo.action;


import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.veo.entity.Admin;
import com.veo.entity.User;
import com.veo.service.AdminService;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("adminAction")
@Scope("prototype")
@ParentPackage("json-default")
@Namespace("/")
@Results({
        @Result(name = "success",type = "json",params = {"root","resultMapToAdmins"}),
        @Result(name = "ok",type = "json",params = {"root","resultMap"}),
})
public class AdminAction extends ActionSupport implements ModelDriven<Admin> {

    private Admin admin=new Admin();

    private List<Admin> resultMapToAdmins;

    private Map<String,String> resultMap;

    public Map<String, String> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, String> resultMap) {
        this.resultMap = resultMap;
    }

    public List<Admin> getResultMapToAdmins() {
        return resultMapToAdmins;
    }

    public void setResultMapToAdmins(List<Admin> resultMapToAdmins) {
        this.resultMapToAdmins = resultMapToAdmins;
    }

    @Autowired
    private AdminService adminService;


    @Override
    public Admin getModel() {
        return admin;
    }

    @Action("getAllAdmin")
    public String getAllAdmin(){
        resultMapToAdmins=adminService.getAllAdmin();
        return SUCCESS;
    }

    @Action("addAdmin")
    public String addAdmin(){
        resultMap=new HashMap<String,String>();
        try {
            adminService.addAdmin(admin);
            resultMap.put("msg","ok");
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }

    @Action("deleteAdmin")
    public String deleteAdmin(){
        resultMap=new HashMap<String,String>();
        try {
            adminService.deleteAdmin(admin);
            resultMap.put("msg","ok");
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }

    @Action("updateAdmin")
    public String updateAdmin(){
        resultMap=new HashMap<String,String>();
        try {
            adminService.updateAdmin(admin);
            resultMap.put("msg","ok");
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }

    @Action("findAdminName")
    public String findUsername(){
        resultMap=new HashMap<String,String>();
        try {
            boolean flag = adminService.findUsername(admin);
            if (flag){
                resultMap.put("msg","true");
            }
            else {
                resultMap.put("msg","false");
            }
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }

    @Action("updateAdminInfo")
    public String updateAdminInfo(){
        adminService.updateAdmin(admin);
        resultMap=new HashMap<String,String>();
        resultMap.put("msg","ok");
        return "ok";
    }

    @Action("updateAdminPass")
    public String updateAdminPass(){
        resultMap=new HashMap<String,String>();
        Admin oldAdmin = adminService.findLoginAdmin(admin);
        System.out.println(admin);
        System.out.println(oldAdmin);
        if (oldAdmin!=null){
            String newPass = ServletActionContext.getRequest().getParameter("newPass");
            oldAdmin.setPassword(newPass);
            adminService.updateAdmin(oldAdmin);
            resultMap.put("msg","ok");
        }else {
            resultMap.put("msg","PassError");
        }
        return "ok";
    }
}
